package com.ceba.test.modules.admin.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ceba.base.web.dao.IBaseDao;
import com.ceba.base.helper.DBJSONHelper;
import com.ceba.cebautils.StringUtil;
import com.ceba.test.modules.admin.entity.*;
import org.json.JSONObject;

import com.ceba.test.base.constants.TextConstants;

import abc.data.DataRow;
import abc.data.DataTable;
import abc.data.Sql;
import abc.data.SqlSessionManager;
import abc.data.dataSource.SqlDataSource;
import abc.data.store.IStore;
import abc.data.store.StoreManager;
import abc.object.IObject;
import abc.object.XObject;

public class InterfaceSignDao extends IBaseDao {

	public boolean deleteFtpSetting(String id) throws Exception{
		Sql sql = new Sql("delete from i_ftp_rule where ID='"+id+"'");
		return SqlSessionManager.execute(sql);
	}

	/**
	 * 增加ftp配置
	 * @param ftp
	 * @return
	 */
	public boolean addFtp(FTPObject ftp) throws Exception{
		IStore store = StoreManager.create("i_ftp_rule");
		XObject object = new XObject("ID",ftp.getId());
		object.put("FTP_FILE_NAME", ftp.getFtpFileName());
		object.put("FTP_READ_USER", ftp.getFtpReadUser());
		object.put("FTP_READ_PWD", ftp.getFtpReadPwd());
		object.put("FTP_READ_DIR", ftp.getFtpReadDir());
		object.put("FTP_WRITE_USER", ftp.getFtpWriteUser());
		object.put("FTP_WRITE_PWD", ftp.getFtpWritePwd());
		object.put("FTP_WRITE_DIR", ftp.getFtpWriteDir());
		object.put("FTP_URL", ftp.getFtpUrl());
		object.put("FTP_PORT", ftp.getFtpPort());
		object.put("CREATE_TIME", ftp.getCreateTime());
		if(store.insert(object)>0){
			return true;
		}
		return false;
	}

	/**
	 * 更新ftp配置
	 * @param ftp
	 * @return
	 */
	public boolean updateFtp(FTPObject ftp) throws Exception{
		IStore store = StoreManager.create("i_ftp_rule");
	    XObject object = new XObject("ID",ftp.getId());
		object.put("FTP_FILE_NAME", ftp.getFtpFileName());
		object.put("FTP_READ_USER", ftp.getFtpReadUser());
		object.put("FTP_READ_PWD", ftp.getFtpReadPwd());
		object.put("FTP_READ_DIR", ftp.getFtpReadDir());
		object.put("FTP_WRITE_USER", ftp.getFtpWriteUser());
		object.put("FTP_WRITE_PWD", ftp.getFtpWritePwd());
		object.put("FTP_WRITE_DIR", ftp.getFtpWriteDir());
		object.put("FTP_URL", ftp.getFtpUrl());
		object.put("FTP_PORT", ftp.getFtpPort());
		object.put("CREATE_TIME", ftp.getCreateTime());
		if(store.update(object)>0){
			return true;
		}
		return false;
	}

	public JSONObject getFTPListSql(String orderBy, String key, Map<String, Object> dic) throws Exception{
		SqlDataSource dataSource = new SqlDataSource();
		
		if (StringUtil.isEmptyString(orderBy)) {
			orderBy = " CREATE_TIME DESC ";
		}
		Sql sql = null;
		StringBuffer ss = new StringBuffer("select ID,FTP_FILE_NAME,FTP_READ_USER,FTP_READ_PWD,FTP_READ_DIR,FTP_WRITE_USER,"
				+ " FTP_WRITE_PWD,FTP_WRITE_DIR,FTP_URL,FTP_PORT,FTP_FLAG from i_ftp_rule ");
		if(StringUtil.isEmptyString(key)){
			ss.append(" ORDER BY " + orderBy);
			sql = new Sql(ss.toString());
		}else{
			ss.append(" WHERE (FTP_FILE_NAME like '%"+key+"%' OR FTP_READ_USER like '%"+key+"%' "
					+ "OR FTP_WRITE_USER like '%"+key+"%' OR FTP_URL like '%"+key+"%' OR FTP_PORT like '%"+key+"%')  ORDER BY " + orderBy);
			sql = new Sql(ss.toString());
		}
		dataSource.getSelectCommands().add(sql);
		DataTable dt = dataSource.query(0, dic);
		if (dt != null && dt.getRowCount() > 0) {
			return dt.toJSONObject();
		}
		return DBJSONHelper.getEmptyDBJSON();
	}

	/**
	 * 增加一条签名规则：key模式
	 * @param iSign
	 * @return
	 */
	public boolean addIsign(ISign iSign) throws Exception{
		IStore store = StoreManager.create("i_sign_rule");
		XObject object = new XObject("ID",iSign.getId());
		object.put("KEY_TYPE",iSign.getKeyType());
		object.put("ENCODE_SIGNTYPE",iSign.getEncodeSignType());
		object.put("QF_RULE_ID", iSign.getQfRuleId());
		object.put("CREDENT_FLAG", iSign.getCredentialsFlag());
		object.put("CREDENT_ID", iSign.getCredentialsId());
		object.put("CREATE_TIME", iSign.getCreateTime());
		
		object.put("MERGE_ID", iSign.getMergeId());
		object.put("WATER_STAMP_ID", iSign.getWaterStampId());
		object.put("SIGN_STAMP_ID", iSign.getSignStampId());
		object.put("OTHER_STAMP_ID", iSign.getOtherStampId());
		object.put("CERTIFICATION_LEVEL", iSign.getCertificationLevel());
		if(store.insert(object)>0){
			return true;
		}
		return false;
	}
	
	public boolean addIsignThree(ISign iSign) throws Exception{
		IStore store = StoreManager.create("i_sign_rule");
		XObject object = new XObject("ID",iSign.getId());
	
		object.put("MERGE_ID", iSign.getMergeId());
		object.put("WATER_STAMP_ID", iSign.getWaterStampId());
		object.put("SIGN_STAMP_ID", iSign.getSignStampId());
		object.put("OTHER_STAMP_ID", iSign.getOtherStampId());
		object.put("QF_RULE_ID", iSign.getQfRuleId());
		object.put("SERVERURL_SUFFIX", iSign.getServerUrlSuffix());
		object.put("CERTIFICATION_LEVEL", iSign.getCertificationLevel());
		object.put("CER_NO", iSign.getCerNo());
		object.put("KEY_NAME", iSign.getKeyName());
		object.put("ENCODE_SIGNTYPE",iSign.getEncodeSignType());
		object.put("KEY_TYPE",iSign.getKeyType());
		object.put("MULTI_SERVER",iSign.getMultiServer());
		object.put("SERVER_PORT",iSign.getServerPort());
		object.put("SERVER_IP",iSign.getServerIp());
		object.put("ID",iSign.getId());
		object.put("ALLOW_PDF_PRINT",iSign.getAllowPdfPrint());
		object.put(" ALLOW_PDF_CONTENT_COPY",iSign.getAllowPdfContentCopy());

		object.put("CREATE_TIME", iSign.getCreateTime());
		object.put("CREATE_USER", !StringUtil.isEmptyString(iSign.getCreateUser())?iSign.getCreateUser():TextConstants.adminId);
		if(store.insert(object)>0){
			return true;
		}
		return false;
	}
	
	
	
	/**
	 *
	 * @return
	 */
	public boolean addCusResouse(CustomResource cusrule) throws Exception{
		IStore store = StoreManager.create("i_cus_rule");
		XObject object = new XObject("ID",cusrule.getId());
		object.put("RULE_NAME", cusrule.getCusfaceName());
		object.put("RULE_PATH", cusrule.getCusPath());
		object.put("CREATE_TIME", cusrule.getCreateTime());
		if(store.insert(object)>0){
			return true;
		}
		return false;
	}
	
	
	public boolean addQiFengRule(QfRule cusrule) throws Exception{
		IStore store = StoreManager.create("i_qifeng_rule");
		XObject object = new XObject("ID",cusrule.getId());
		object.put("RULE_NAME", cusrule.getCusfaceName());
		object.put("IMG_WIDTH", cusrule.getImgWidth());
		object.put("IMG_HEIGHT", cusrule.getImgHeight());
		object.put("VERTICALAPEAK", cusrule.getCusVerticalApeak());
		object.put("POSITION", cusrule.getCusPosition());
		object.put("ODEVITY", cusrule.getOdevity());
		object.put("SYNCOPATE", cusrule.getSyncopate());
		object.put("FRIST_APEAK", cusrule.getFristApeak());
		object.put("OUTHER_APEAK", cusrule.getOutherApeak());
		object.put("FILE_PATH", cusrule.getFilePath());
		object.put("CREATE_TIME", cusrule.getCreateTime());
		object.put("STORAGE_ADDRESS_ID",cusrule.getStorageAddressId());
		if(store.insert(object)>0){
			return true;
		}
		return false;
	}
	
	
	/**
	 *
	 * @return
	 */
	public boolean updateCusResouse(CustomResource cusrule) throws Exception{
		IStore store = StoreManager.create("i_cus_rule");
		XObject object = new XObject("ID",cusrule.getId());
		object.put("RULE_NAME", cusrule.getCusfaceName());
		object.put("CREATE_TIME", cusrule.getCreateTime());
		if(store.update(object)>0){
			return true;
		}
		return false;
	}
	
	
	
	public boolean updateQiFengRule(QfRule cusrule) throws Exception{
		IStore store = StoreManager.create("i_qifeng_rule");
		XObject object = new XObject("ID",cusrule.getId());
		object.put("RULE_NAME", cusrule.getCusfaceName());
		object.put("IMG_WIDTH", cusrule.getImgWidth());
		object.put("IMG_HEIGHT", cusrule.getImgHeight());
		object.put("VERTICALAPEAK", cusrule.getCusVerticalApeak());
		object.put("POSITION", cusrule.getCusPosition());
		object.put("ODEVITY", cusrule.getOdevity());
		object.put("SYNCOPATE", cusrule.getSyncopate());
		object.put("FRIST_APEAK", cusrule.getFristApeak());
		object.put("OUTHER_APEAK", cusrule.getOutherApeak());
		object.put("STORAGE_ADDRESS_ID",cusrule.getStorageAddressId());
		if(cusrule.getFilePath()!=null){
			object.put("FILE_PATH", cusrule.getFilePath());
		}
		object.put("CREATE_TIME", cusrule.getCreateTime());
		if(store.update(object)>0){
			return true;
		}
		return false;
	}
	
	
	public CustomResource getByIdCusResouse(String cusruleId) throws Exception{
		CustomResource customResource = new CustomResource();
		IStore store = StoreManager.create("i_cus_rule");
		IObject iobject = store.get(cusruleId);
		if(iobject!=null){
			customResource.setId(iobject.getString("ID"));
			customResource.setCusfaceName(iobject.getString("RULE_NAME"));
		}
		
		return customResource;
	}

	/**
	 * 用户名比对，存在返回 true
	 * 不存在返回false
	 * @param id
	 * @param name
	 * @return
	 */
	public boolean queryQifengByNameAndId(String id,String name){
		Sql sql = new Sql("select ID,RULE_NAME FROM i_qifeng_rule where RULE_NAME='"+name+"' and ID <> '"+id+"'");
		DataTable dataTable = SqlSessionManager.getDataTable(sql);
		if(dataTable!=null&&dataTable.getRowCount()>0){
			return true;
		}else{
			return false;
		}
	}
	
	
	public QfRule getQiFengRuleById(String cusruleId) throws Exception{
		QfRule customResource = new QfRule();
		IStore store = StoreManager.create("i_qifeng_rule");
		IObject iobject = store.get(cusruleId);
		if(iobject!=null){
			customResource.setId(iobject.getString("ID"));
			customResource.setCusfaceName(iobject.getString("RULE_NAME"));
			customResource.setCusPosition(Integer.parseInt(iobject.getString("POSITION")));
			customResource.setCusVerticalApeak(Integer.parseInt(iobject.getString("VERTICALAPEAK")));
			customResource.setOdevity(Integer.parseInt(iobject.getString("ODEVITY")));
			customResource.setSyncopate(Integer.parseInt(iobject.getString("SYNCOPATE")));
			customResource.setFristApeak(Integer.parseInt(iobject.getString("FRIST_APEAK")));
			customResource.setOutherApeak(Integer.parseInt(iobject.getString("OUTHER_APEAK")));
			customResource.setImgWidth(Float.parseFloat(iobject.getString("IMG_WIDTH")));
			customResource.setImgHeight(Float.parseFloat(iobject.getString("IMG_HEIGHT")));
			customResource.setFilePath(iobject.getString("FILE_PATH"));
			customResource.setStorageAddressId(iobject.getString("STORAGE_ADDRESS_ID"));
		}
		
		return customResource;
	}
	
	
	
	public boolean updateIsign1(ISign iSign) throws Exception{
		IStore store = StoreManager.create("i_sign_rule");
		XObject object = new XObject("ID",iSign.getId());
		object.put("MERGE_ID", iSign.getMergeId());
		object.put("WATER_STAMP_ID", iSign.getWaterStampId());
		object.put("SIGN_STAMP_ID", iSign.getSignStampId());
		object.put("OTHER_STAMP_ID", iSign.getOtherStampId());
		object.put("QF_RULE_ID", iSign.getQfRuleId());
		object.put("SERVERURL_SUFFIX", iSign.getServerUrlSuffix());
		object.put("CERTIFICATION_LEVEL", iSign.getCertificationLevel());
		object.put("CER_NO", iSign.getCerNo());
		object.put("KEY_NAME", iSign.getKeyName());
		object.put("ENCODE_SIGNTYPE",iSign.getEncodeSignType());
		object.put("KEY_TYPE",iSign.getKeyType());
		object.put("MULTI_SERVER",iSign.getMultiServer());
		object.put("SERVER_PORT",iSign.getServerPort());
		object.put("SERVER_IP",iSign.getServerIp());
		object.put(" ALLOW_PDF_PRINT",iSign.getAllowPdfPrint());
		object.put("ALLOW_PDF_CONTENT_COPY",iSign.getAllowPdfContentCopy());
		if(store.update(object)>0){
			return true;
		}
		return false;
	}

	
	/**
	 * 服务器模式下
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public ISign getInterfaceSignById1(String id) throws Exception {
		ISign iSign = new ISign();
		
		Sql sql = new Sql("SELECT ID,SERVER_IP,SERVER_PORT,MULTI_SERVER,KEY_TYPE,ALLOW_PDF_CONTENT_COPY,ALLOW_PDF_PRINT,ENCODE_SIGNTYPE,CER_NO,KEY_NAME,CERTIFICATION_LEVEL,SERVERURL_SUFFIX,QF_RULE_ID,OTHER_STAMP_ID,SIGN_STAMP_ID,WATER_STAMP_ID,MERGE_ID,CREATE_TIME,CREATE_USER FROM i_sign_rule where ID =?");
		sql.addParameter(id);
		DataTable dt = SqlSessionManager.getDataTable(sql);
		if(dt !=null && dt.getRowCount()>0){
			DataRow dr = dt.getRow(0);
			iSign.setId(dr.getString("ID"));
			iSign.setServerIp(dr.getString("SERVER_IP"));
			iSign.setServerPort(dr.getInt("SERVER_PORT"));
			iSign.setMultiServer(dr.getString("MULTI_SERVER"));
			iSign.setKeyType(dr.getInt("KEY_TYPE"));
			iSign.setEncodeSignType(dr.getInt("ENCODE_SIGNTYPE"));
			iSign.setCerNo(dr.getString("CER_NO"));
			iSign.setKeyName(dr.getString("KEY_NAME"));
			iSign.setCertificationLevel(dr.getInt("CERTIFICATION_LEVEL"));
			iSign.setServerUrlSuffix(dr.getString("SERVERURL_SUFFIX"));
			iSign.setQfRuleId(dr.getString("QF_RULE_ID"));
			iSign.setOtherStampId(dr.getString("OTHER_STAMP_ID"));
			iSign.setSignStampId(dr.getString("SIGN_STAMP_ID"));
			iSign.setWaterStampId(dr.getString("WATER_STAMP_ID"));
			iSign.setMergeId(dr.getString("MERGE_ID"));
			iSign.setStampID(dr.getString("SIGN_STAMP_ID"));
			iSign.setCreateTime(dr.getString("CREATE_TIME"));
			iSign.setAllowPdfContentCopy(dr.getInt("ALLOW_PDF_CONTENT_COPY"));
			iSign.setAllowPdfPrint(dr.getInt("ALLOW_PDF_PRINT"));
		}else{
			return null;
		}
		return iSign;
		
	}
	
	public boolean deleteIsign(String id) throws Exception{
		Sql sql = new Sql("delete from i_sign_rule where ID='"+id+"'");
		return SqlSessionManager.execute(sql);
	}
	
	
	public boolean deleteIsCus(String id) throws Exception{
		Sql sql = new Sql("delete from i_cus_rule where ID='"+id+"'");
		return SqlSessionManager.execute(sql);
	}
	
	public boolean deleteqiFengRule(String id) throws Exception{
		Sql sql = new Sql("delete from i_qifeng_rule where ID='"+id+"'");
		return SqlSessionManager.execute(sql);
	}
	
	public boolean queryQiFengRuelByName(String name){
		boolean flag = true;
		Sql sql = new Sql("select ID,RULE_NAME from i_qifeng_rule where RULE_NAME='"+name+"'");
		 long count = SqlSessionManager.getCount(sql);
		 if(count>0){
			 flag = false; 
		 }
		return flag;
	}
	
	public String getCusRulePath(String ruleId){
		IStore store = StoreManager.create("i_cus_rule");
		IObject object = store.get(ruleId);
 		  String path =object.getString("RULE_PATH");
 		  return path;
	}
	
	
public JSONObject getInterfaceSignListSql1(String orderBy, String key, Map<String, Object> dic) throws Exception{
		SqlDataSource dataSource = new SqlDataSource();
		
		if (StringUtil.isEmptyString(orderBy)) {
			orderBy = " CREATE_TIME DESC ";
		}
		Sql sql = null;
		StringBuffer ss = new StringBuffer("SELECT ID,SERVER_IP,SERVER_PORT,MULTI_SERVER,KEY_TYPE,ENCODE_SIGNTYPE,CER_NO,KEY_NAME,CERTIFICATION_LEVEL,SERVERURL_SUFFIX,QF_RULE_ID,OTHER_STAMP_ID,SIGN_STAMP_ID,WATER_STAMP_ID,MERGE_ID,CREATE_TIME,CREATE_USER FROM i_sign_rule ");
			if(StringUtil.isEmptyString(key)){
				ss.append(" ORDER BY " + orderBy);
				sql = new Sql(ss.toString());
			}else{
				ss.append(" WHERE (ID like '%"+key+"%' OR SERVER_IP like '%"+key+"%' OR SERVER_PORT like '%"+key+"%' OR MULTI_SERVER like '%"+key+"%')  ORDER BY " + orderBy);
				sql = new Sql(ss.toString());
			}
			
		
	dataSource.getSelectCommands().add(sql);
	DataTable dt = dataSource.query(0, dic);
	if (dt != null && dt.getRowCount() > 0) {
		return dt.toJSONObject();
	}
	return DBJSONHelper.getEmptyDBJSON();
		
	}
	
	//骑缝章规则查询
	public JSONObject getCusfaceListSql(String key,String orderBy,Map<String,Object> dic) throws Exception{
		SqlDataSource dataSource = new SqlDataSource();
		
		if (StringUtil.isEmptyString(orderBy)) {
			orderBy = " CREATE_TIME DESC ";
		}

		Sql sql = null;
		StringBuffer ss = new StringBuffer("SELECT * FROM i_qifeng_rule s");
		
		if(StringUtil.isEmptyString(key)){
			ss.append("  ORDER BY " + orderBy);
			sql = new Sql(ss.toString());
		}else{
			ss.append(" WHERE (s.RULE_NAME like '%"+key+"%')  ORDER BY " + orderBy );
			sql = new Sql(ss.toString());
		}
		dataSource.getSelectCommands().add(sql);
		DataTable dt = dataSource.query(0, dic);
		if (dt != null && dt.getRowCount() > 0) {
			JSONObject jsonObject = dt.toJSONObject();
			jsonObject.put("total",getCusfaceListcount(key));
			return jsonObject;
		}
		return DBJSONHelper.getEmptyDBJSON();
		
	}


	public JSONObject getCusfaceListSqlOne(String key,String orderBy,Map<String,Object> dic) throws Exception{
		if (StringUtil.isEmptyString(orderBy)) {
			orderBy = " CREATE_TIME DESC ";
		}

		Sql sql = null;
		StringBuffer ss = new StringBuffer("SELECT * FROM i_qifeng_rule s");

		if(StringUtil.isEmptyString(key)){
			ss.append("  ORDER BY " + orderBy);
			sql = new Sql(ss.toString());
		}else{
			ss.append(" WHERE (s.RULE_NAME like '%"+key+"%')  ORDER BY " + orderBy );
			sql = new Sql(ss.toString());
		}
		DataTable dt = SqlSessionManager.getDataTable(sql);
		if (dt != null && dt.getRowCount() > 0) {
			JSONObject jsonObject = dt.toJSONObject();
			jsonObject.put("total",getCusfaceListcount(key));
			return jsonObject;
		}
		return DBJSONHelper.getEmptyDBJSON();

	}

	public int getCusfaceListcount(String key) throws Exception{
		SqlDataSource dataSource = new SqlDataSource();
		Sql sql = null;
		StringBuffer ss = new StringBuffer("SELECT count(*) FROM i_qifeng_rule s");

		if(StringUtil.isEmptyString(key)){
			sql = new Sql(ss.toString());
		}else{
			ss.append(" WHERE (s.RULE_NAME like '%"+key+"%')");
			sql = new Sql(ss.toString());
		}
		DataTable dt = SqlSessionManager.getDataTable(sql);
		if(dt!=null&&dt.getRowCount()>0){
			return dt.getRow(0).getInt("COUNT(*)");
		}
		return 0;
	}

	/**
	 *
	 * 根据用户id获取授权章
	 *
	 */

	public JSONObject queryQifengAu(String userId,Map<String,Object> dic) {
		Sql sql = new Sql(
				"select i_qifeng_rule.ID,RULE_NAME AS RULE_NAME from i_qifeng_rule where i_qifeng_rule.ID in (select g_user_cor_stamp.USER_AUTHORIZATION_ID  from "+
						" g_user_cor_stamp where g_user_cor_stamp.USER_ID='"+userId+"')");
		DataTable dt = SqlSessionManager.getDataTable(sql);
		if(dt !=null && dt.getRowCount() >0){
			return dt.toJSONObject();
		}else{
			return new JSONObject();
		}
	}

	public long queryQifengAuTotal(String userId,Map<String,Object> dic) {
		Sql sql = new Sql(
				"select i_qifeng_rule.ID,RULE_NAME AS RULE_NAME from i_qifeng_rule where i_qifeng_rule.ID in (select g_user_cor_stamp.USER_AUTHORIZATION_ID  from "+
						" g_user_cor_stamp where g_user_cor_stamp.USER_ID='"+userId+"')");
		DataTable dt = SqlSessionManager.getDataTable(sql);
		return dt.getRowCount();
	}

	public void parseDrToStamp(DataRow dr,QfRule qfRule) {
		qfRule.setId(dr.getString("ID"));
		qfRule.setCusfaceName(dr.getString("RULE_NAME"));
		qfRule.setFilePath(dr.getString("RULE_PATH"));
		qfRule.setCusVerticalApeak(dr.getInt("VERTICALAPEAK"));
		qfRule.setOdevity(dr.getInt("ODEVITY"));
		qfRule.setSyncopate(dr.getInt("SYNCOPATE"));
		qfRule.setFristApeak(dr.getInt("FRIST_APEAK"));
		qfRule.setOutherApeak(dr.getInt("OUTHER_APEAK"));
		qfRule.setStorageAddressId(dr.getString("STORAGE_ADDRESS_ID"));
	}

	//骑缝章规则查询
	public List<QfRule> getCusfaceListSqlOne(String orderBy, String key) throws Exception{
		SqlDataSource dataSource = new SqlDataSource();

		if (StringUtil.isEmptyString(orderBy)) {
			orderBy = " CREATE_TIME DESC ";
		}
		Sql sql = null;
		StringBuffer ss = new StringBuffer("SELECT * FROM i_qifeng_rule s");
		if(StringUtil.isEmptyString(key)){
			ss.append("  ORDER BY " + orderBy);
			sql = new Sql(ss.toString());
		}else{
			ss.append(" WHERE (s.RULE_NAME like '%"+key+"%')  ORDER BY " + orderBy);
			sql = new Sql(ss.toString());
		}
		DataTable dt = SqlSessionManager.getDataTable(sql);
		List<QfRule> list = new ArrayList<QfRule>();
		if(dt !=null && dt.getRowCount()>0){
			for(int i = 0 ; i < dt.getRowCount() ; i ++){
				DataRow dr = dt.getRow(i);
				QfRule qfRule = new QfRule();
				parseDrToStamp(dr, qfRule);
				list.add(qfRule);
			}
		}
		return list;
	}


	/**
	 * 根据用户id获取骑缝章缩缩图
	 */
	public  DataTable getUserDigitalStampPicList(String userId) throws Exception{
		StringBuffer s = new StringBuffer("select i_qifeng_rule.FILE_PATH ,i_qifeng_rule.STORAGE_ADDRESS_ID from g_user_cor_stamp LEFT OUTER JOIN i_qifeng_rule "+
				" ON g_user_cor_stamp.USER_AUTHORIZATION_ID = i_qifeng_rule.ID"+
				" where g_user_cor_stamp.USER_ID='"+userId+"'and g_user_cor_stamp.AUTHORIZATION_TYPE=2 ");
		Sql sql  = new Sql(s.toString());
		return SqlSessionManager.getDataTable(sql);
	}

	//骑缝章规则查询
	public List<QfRule> getCusfaceListSqlUserId(String userId) throws Exception{
		SqlDataSource dataSource = new SqlDataSource();

		Sql sql = new Sql("SELECT qf.ID,qf.RULE_NAME, qf.FILE_PATH,qf.POSITION, qf.VERTICALAPEAK,qf.ODEVITY, qf.SYNCOPATE,qf.FRIST_APEAK, qf.OUTHER_APEAK,qf.FILE_PATH FROM i_qifeng_rule as qf"
				+ "	LEFT OUTER JOIN g_user_cor_stamp as gu ON gu.USER_AUTHORIZATION_ID = qf.ID "
				+ " WHERE gu.USER_ID = '"+ userId + "'and gu.AUTHORIZATION_TYPE='2' "
				+ " GROUP BY qf.RULE_NAME,qf.ID,qf.FILE_PATH,qf.POSITION, qf.VERTICALAPEAK,qf.ODEVITY, qf.SYNCOPATE,qf.FRIST_APEAK, qf.OUTHER_APEAK,qf.FILE_PATH");
		DataTable dt = SqlSessionManager.getDataTable(sql);
		List<QfRule> list = new ArrayList<QfRule>();
		if(dt !=null && dt.getRowCount()>0){
			for(int i = 0 ; i < dt.getRowCount() ; i ++){
				DataRow dr = dt.getRow(i);
				QfRule qfRule = new QfRule();
				parseDrToStamp(dr, qfRule);
				list.add(qfRule);
			}
		}
		return list;
	}

	public boolean getICusRuleByName(String name){
		DataTable dt=SqlSessionManager.getDataTable("select * from i_cus_rule where RULE_NAME = '"+name+"'");
		if(dt !=null && dt.getRowCount() >0){
			return false;
		}
		return true;
	}
	
	//骑缝章规则查询
	public JSONObject getICusRule(String orderBy, String key,Map<String, Object> dic) throws Exception{
		SqlDataSource dataSource = new SqlDataSource();

		if (StringUtil.isEmptyString(orderBy)) {
			orderBy = " CREATE_TIME DESC ";
		}

		Sql sql = null;
		StringBuffer ss = new StringBuffer("SELECT * FROM i_cus_rule s");

		if(StringUtil.isEmptyString(key)){
			ss.append("  ORDER BY " + orderBy);
			sql = new Sql(ss.toString());
		}else{
			ss.append(" WHERE (s.RULE_NAME like '%"+key+"%')  ORDER BY " + orderBy);
			sql = new Sql(ss.toString());
		}
		dataSource.getSelectCommands().add(sql);
		DataTable dt = dataSource.query(0, dic);
		if (dt != null && dt.getRowCount() > 0) {
			return dt.toJSONObject();
		}
		return DBJSONHelper.getEmptyDBJSON();

	}
	
	
	public List<CustomResource> getQfRule() throws Exception{
		List<CustomResource> list = new ArrayList<CustomResource>();
		Sql sql=new Sql("SELECT * FROM i_cus_rule WHERE RULE_TYPE= 1   ORDER BY CREATE_TIME DESC  ");
		DataTable dt= SqlSessionManager.getDataTable(sql);
		if(dt != null && dt.getRowCount()>0){
			for (int i = 0; i <dt.getRowCount() ; i++) {
				CustomResource resource  = new CustomResource();
				resource.setId(dt.getRow(i).getString("ID"));
				resource.setCusfaceName(dt.getRow(i).getString("RULE_NAME"));
				list.add(resource);
			}
		}
		
		return list;
	}
	
	
	public FTPObject getFTPObjectById(String id) {
		FTPObject ftpSetting = new FTPObject();
		Sql sql = new Sql("SELECT ID,FTP_FILE_NAME,FTP_READ_USER,FTP_READ_PWD,FTP_READ_DIR,FTP_WRITE_USER,FTP_WRITE_PWD,"
				+ " FTP_WRITE_DIR,FTP_URL,FTP_PORT,CREATE_TIME FROM i_ftp_rule WHERE ID=?");
		sql.addParameter(id);
		DataTable dt = SqlSessionManager.getDataTable(sql);
		if(dt !=null && dt.getRowCount()>0){
			DataRow dr = dt.getRow(0);
			ftpSetting.setId(dr.getString("ID"));
			ftpSetting.setFtpFileName(dr.getString("FTP_FILE_NAME"));
			ftpSetting.setFtpReadUser(dr.getString("FTP_READ_USER"));
			ftpSetting.setFtpReadPwd(dr.getString("FTP_READ_PWD"));
			ftpSetting.setFtpReadDir(dr.getString("FTP_READ_DIR"));
			ftpSetting.setFtpWriteUser(dr.getString("FTP_WRITE_USER"));
			ftpSetting.setFtpWritePwd(dr.getString("FTP_WRITE_PWD"));
			ftpSetting.setFtpWriteDir(dr.getString("FTP_WRITE_DIR"));
			ftpSetting.setFtpUrl(dr.getString("FTP_URL"));
			ftpSetting.setFtpPort(dr.getString("FTP_PORT"));
			ftpSetting.setCreateTime(dr.getString("CREATE_TIME"));
			return ftpSetting;
		}else{
			return null;
		}
		
	}


}
